package net.thxjj.adminServer.service.impl;

import java.util.HashMap;
import java.util.List;

import net.thxjj.adminServer.dao.CmdationDao;
import net.thxjj.sitermodel.Cmdation;
import net.thxjj.sitermodel.ParamProperty;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.thxjj.adminServer.dao.ParamDao;
import net.thxjj.adminServer.entity.ResponseModel;
import net.thxjj.adminServer.entity.ResultEnum;
import net.thxjj.adminServer.service.IParamService;
import net.thxjj.adminServer.util.ParameterMap;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class ParamService implements IParamService{
	private Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private ParamDao paramDao;
	@Autowired
	private CmdationDao cmdationDao;


	@Override
	public List<ParamProperty> list(String prod_key) {
		return paramDao.list(prod_key);
	}

	@Override
	public ParamProperty getParamInfo(long id) {
		// TODO Auto-generated method stub
		return paramDao.getParamInfo(id);
	}

	@Override
	public ParamProperty getParamInfoByProdAndParam(String prod_key, String identifier) {
		return paramDao.getParamInfoByProdAndParam(prod_key,identifier);
	}


	@Override
	public HashMap<String, Object> addParam(ParameterMap pm) {
		// TODO Auto-generated method stub
		try {
			paramDao.addParam(pm);
		}catch(Exception e) {
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel(ResultEnum.ERROR, null);
		}
		return ResponseModel.getModel(ResultEnum.SUCCESS, null);
	}



	@Override
	public HashMap<String, Object> edit(ParameterMap pm) {
		// TODO Auto-generated method stub
		try {
			paramDao.editParam(pm);
		}catch(Exception e) {
			log.error("error:"+e.getMessage(), e);
			return ResponseModel.getModel(ResultEnum.ERROR, null);
		}
		return ResponseModel.getModel(ResultEnum.SUCCESS, null);
	}



	@Override
	public HashMap<String, Object> del(long id) {
		// TODO Auto-generated method stub
		try
		{
			ParamProperty paramProperty = paramDao.getParamInfo(id);
			String prod_key = paramProperty.getProdKey();
			List<Cmdation> cmdationList  = cmdationDao.listSorted(prod_key,id);

			for (Cmdation cmdation:cmdationList){
				String content = cmdation.getContent();
				if(content.contains(","+id)){
					content=content.replace(","+id,"");
				}else if(content.contains(id+",")){
					content=content.replace(id+",","");
				}
				cmdationDao.updateContent(content,cmdation.getProdKey(),cmdation.getCmdId());
			}

			paramDao.delParam(id);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return ResponseModel.getModel(ResultEnum.ERROR, null);
		}

		return ResponseModel.getModel(ResultEnum.SUCCESS, null);
	}



	@Override
	public List<ParamProperty> getParamInfos(ParameterMap pm) {
		// TODO Auto-generated method stub
		return paramDao.getParamInfos(pm);
	}

}
